﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DataAccessLayer.Security;
using Common;
namespace DataAccessLayer.DAL
{
    public class ConfigurationDAL
    {
        /// <summary>
        /// Them thông tin Configuration
        /// </summary>
        /// <param name="user">ConfigurationDAL to save</param>
        public static void SaveConfigurationDAL(Configuration configuration)
        {
            using (var context = new ReverseAuctionEntities())
            {
                context.Configurations.AddObject(configuration);
                context.SaveChanges();
            }
        }

        // Lay danh sach Configuration
        public static List<Configuration> GetAllConfiguration()
        {
            using (var context = new ReverseAuctionEntities())
            {
                return context.Configurations.ToList();
            }
        }
        // xoa
     
        public static void DeteleConfigurationDAL(String name)
        {
            using (var context = new ReverseAuctionEntities())
            {
                Configuration user = context.Configurations.SingleOrDefault(c => c.Name == name);
                if (user != null)
                {
                    context.Configurations.DeleteObject(user);
                    context.SaveChanges();
                }
            }
        }

        public static void DeleteConfigurationID(int id)
        {
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.Configurations.SingleOrDefault(u => u.ID == id);
                 if (result != null)
                {
                context.Configurations.DeleteObject(result);
                context.SaveChanges();
                 }
            }
        }

        public static void UpdateConfiguration(Configuration con)
        {
            using (var context = new ReverseAuctionEntities())
            {
                Configuration result = context.Configurations.SingleOrDefault(u => u.ID == con.ID);
                if (result != null)
                {
                    result.Detail = con.Detail;
                    context.Configurations.Attach(result);
                    context.ObjectStateManager.ChangeObjectState(result,System.Data.EntityState.Modified);
                    context.SaveChanges();
                }
            }
        }

        public static void Update(Configuration con)
        {
            using (var context = new ReverseAuctionEntities())
            {
                Configuration result = context.Configurations.SingleOrDefault(u => u.ID == con.ID);
                //result.Name = con.Name;
                //result.Detail = con.Detail;
                if (result != null)
                {
                    result.Detail = con.Detail;
                    //context.Configurations.Attach(result);
                    //context.ObjectStateManager.ChangeObjectState(result, System.Data.EntityState.Modified);
                    context.SaveChanges();
                }
            }
        }
        public static int SearchID(string name)
        {
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.Configurations.SingleOrDefault(u => u.Name == name);
                return result.ID;
            }
        }

        public static string SearchName(int id)
        {
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.Configurations.SingleOrDefault(u => u.ID == id);
                string data = null;
                if (result != null)
                {
                    data = result.Detail;

                }
                return data;
            }
        }
        public static string SearchDetail(int id)
        {
            using (var context = new ReverseAuctionEntities())
            {
                var result = context.Configurations.SingleOrDefault(u => u.ID == id);
                string data = null;
                if (result != null)
                {
                    data = result.Detail;
                    
                }
                return data;
            }
        }

        public static Configuration GetByName(string name)
        {
            using (var context = new ReverseAuctionEntities())
            {
                return context.Configurations.SingleOrDefault(u => u.Name == name);
            }
        }
    }
}
